package com.starbaba.creditcard.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.jumai.server.qiniu.QiniuUploadUtils;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.creditcard.model.Loan_Credit_Card_Info;
import com.starbaba.creditcard.model.Loan_Credit_Card_InfoVO;
import com.starbaba.creditcard.service.LoanCreditCareService;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.tools.sequence.Sequence;

@Component
public class LoanCreditCareServiceImpl implements LoanCreditCareService {
	@SuppressWarnings("deprecation")
	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	@Override
	public void saveCreditCard(Loan_Credit_Card_InfoVO model) {

		try {

			Loan_Credit_Card_Info info = this.setValue(model);
			int position = this.getPosition();
			info.setPosition(position);
			dao.add(info);

		} catch (AppException e) {
			LogService.error(e);
		}
	}

	@Override
	public void updateCreditCard(Loan_Credit_Card_InfoVO model) {
		Loan_Credit_Card_Info info = this.setValue(model);

		try {
			dao.update(info, new String[] { "id" });
		} catch (AppException e) {
			LogService.error(e);
		}
	}

	private int getPosition() throws AppException {
		int position = 0;
		String sql = "select position from loan_credit_card_info order by position desc limit 1";
		position = dao.getInt(sql, new Object[] {});
		return position;
	}

	public Loan_Credit_Card_Info setValue(Loan_Credit_Card_InfoVO bean) {

		Loan_Credit_Card_Info model = null;
		try {
			model = new Loan_Credit_Card_Info();
			String thumbnail = null;
			File thumbnailFile = bean.getImg();
			if (thumbnailFile != null) {
				thumbnail = uploadToQiniu(thumbnailFile);
				model.setLogo(thumbnail);
			}
			Long id = bean.getId();
			if (null == id) {
				model.setId(Sequence.getSequence());
			} else {
				model.setId(bean.getId());
			}
			model.setSummary(bean.getSummary());
			model.setName(bean.getName());
			model.setUrl(bean.getUrl());
			model.setShow_state(bean.getShow_state());
			model.setTag(bean.getTag());
			model.setInitcount(bean.getInitcount());
			model.setPosition(bean.getPosition());
			model.setPrdid(bean.getPrdid());
			model.setStarttime(bean.getStarttime());
			model.setEndtime(bean.getEndtime());
			model.setStatus(bean.getStatus());
			model.setUrl(bean.getUrl());
			model.setOperator(bean.getOperator());
			model.setAdvert_space(bean.getAdvert_space());
			model.setPlatform(bean.getPlatform());
			model.setChannel(bean.getChannel());
			model.setSuccess_count(bean.getSuccess_count());
			model.setPrompt(bean.getPrompt());
			model.setSummary_add(bean.getSummary_add());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	public String uploadToQiniu(File srcFile) {
		String iconUrl = null;
		if (srcFile.isDirectory()) {
			return iconUrl;
		} else {
			try {
//				iconUrl = QiniuService.uploadImg2Qiniu(srcFile.getAbsolutePath(), "loan");
				FileInputStream in = new FileInputStream(srcFile);
				// Zone为七牛机房所在地，根据源所在机房配置
		        Configuration config = new Configuration(Zone.zone0()); 
		        iconUrl = QiniuUploadUtils.updateImg("loan", in, null, config);
		        in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return iconUrl;
	}

	
	@SuppressWarnings("rawtypes")
	@Override
	public List<Loan_Credit_Card_InfoVO> getDataList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap) {
		String platform = (String) paramsMap.get("platform");
		String status = (String) paramsMap.get("status");
		String name = (String) paramsMap.get("name");
		String channel = (String) paramsMap.get("channel");
		String tag = (String) paramsMap.get("tag");
		String advert_space = (String) paramsMap.get("advert_space");
		String prdid = (String) paramsMap.get("prdid");

		String sql = "select c.channel,c.platform,c.position,c.id,c.operator,c.starttime,c.position,c.status,c.name,c.logo from loan_credit_card_info c where 1=1";
		String selectSql = "";
		if (!StringUtils.isEmpty(platform)) {
			selectSql += " and c.platform = " + platform;
		}
		if (!StringUtils.isEmpty(status)) {
			selectSql += " and c.status = " + status;
		}
		if (!StringUtils.isEmpty(name)) {
			selectSql += " and c.name = " + name;
		}

		if (!StringUtils.isEmpty(channel)) {
			selectSql += " and c.channel = " + channel;
		}

		if (!StringUtils.isEmpty(tag)) {
			selectSql += " and c.tag = " + tag;
		}
		if (!StringUtils.isEmpty(advert_space)) {
			selectSql += " and c.advert_space = " + advert_space;
		}
		if (!StringUtils.isEmpty(prdid)) {
			selectSql += " and c.prdid = " + prdid;
		}

		// if (!StringUtils.isEmpty(activityid)) {
		// selectSql += " and c.id = " + activityid ;
		// }
		// if (!StringUtils.isEmpty(name)) {
		// selectSql += " and c.name like '%" +name + "%'";
		// }
		// if (!StringUtils.isEmpty(status)) {
		// selectSql += " and c.status = '" + status + "'";
		// }

		sql += selectSql;
		sql += " order by c.position asc limit " + iDisplayStart + " , " + iDisplayLength;

		String countSql = "select count(1) from loan_credit_card_info c where 1=1 " + selectSql;
		List<Loan_Credit_Card_InfoVO> list = null;
		try {
			int count = dao.getInt(countSql);
			list = dao.list(sql, Loan_Credit_Card_InfoVO.class, new Object[] {});
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return list;
	}

}
